/*************************************************************************
	> File Name: 006.P1507.NASA的食物计划.cpp
	> Author: Maureen 
	> Mail: Maureen@qq.com 
	> Created Time: 二  6/29 12:57:47 2021
 ************************************************************************/

#include <iostream>
using namespace std;

int V, M, N, v[50], m[50], c[50], ans[50][405][405];

int main() {
    cin >> V >> M >> N;
    for (int i = 1; i <= N; i++) {
        cin >> v[i] >> m[i] >> c[i];
    }

    
    for (int i = 1; i <= N; i++) { //循环物品
        for (int j = 1; j <= V; j++) { //循环体积
            for (int k = 1; k <= M; k++) {//循环质量
                if (j < v[i] || k < m[i]) { //装不下第i件物品
                    ans[i][j][k] = ans[i - 1][j][k];
                } else { //装得下第i件物品，在装与不装的方案中选
                    ans[i][j][k] = max(ans[i - 1][j][k], ans[i - 1][j - v[i]][k - m[i]] + c[i]);
                }
            }
        }
    }
    
    cout << ans[N][V][M] << endl;

    return 0;
}
